<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class jzmluvyControllerupravapartner extends JController
{

	function __construct()
	{
		parent::__construct();
		
		// Registrace extra dotazu
		$this->registerTask( 'add'  , 	'display'  );
		$this->registerTask( 'edit'  , 	'display'  );
		$this->registerTask( 'cancel'  , 	'display'  );
		$this->registerTask( 'apply', 	'save'  );
		$this->registerTask( 'remove', 	'delete'  );
		$this->registerTask( 'publish', 	'publish'  );
	}

  	/**
	 * Displays a view
	 */
	function display( )
	{
		switch($this->getTask())
		{
			case 'add'     :
			{	 JRequest::setVar( 'hidemainmenu', 1 );
//				JRequest::setVar( 'layout', 'form'  );
				 JRequest::setVar( 'view', 'upravapartner' );
				 JRequest::setVar( 'edit', false );
				
			} break;
			case 'edit'    :
			{
				$db 		=& JFactory::getDBO();
        
        $id = JRequest::getVar( 'id', '', '', 'array' );
    
        $query = 'SELECT state'
    			. ' FROM #__jzmluvy_partner'
    			. ' WHERE id = '.$id[0]
      		;
    		$db->setQuery( $query );
    		$state = $db->loadResult();
        				
        JRequest::setVar( 'hidemainmenu', 1 );
//				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'upravapartner' );
				JRequest::setVar( 'edit', true );
			} break;
			
			case 'cancel'    :
			{
				JRequest::setVar( 'view', 'partner' );
			} break;
		}

		parent::display();
	}
	
		function save()
	{
		JRequest::checkToken() or die( 'Invalid Token' );
		
		$task		= JRequest::getVar('task');

		$post = JRequest::get( 'post' );
		$post['description'] = JRequest::getVar( 'description', '', 'post','string', JREQUEST_ALLOWRAW );
		$post['description']	= str_replace( '<br>', '<br />', $post['description'] );

		$model = $this->getModel('upravapartner');
		
		$returnid = $model->store($post);

		if ($returnid) {

			switch ($task)
			{
				case 'apply' :
					$link = 'index.php?option=com_jzmluvy&view=upravapartner&hidemainmenu=1&id[]='.$returnid;
					break;

				default :
					$link = 'index.php?option=com_jzmluvy&view=partner';
					break;
			}
			$msg	= JText::_( 'pa_INFO_EDIT' );

		} else {

			$msg 	= '';
			$link = 'index.php?option=com_jzmluvy&view=partner';

		}



//		$model->checkin();
    //*** ak je chybova hlaska tak .......
   
     if (JError::getError()->message != '') {
      $link  = 'index.php?option=com_jzmluvy&view=upravapartner&hidemainmenu=1&task=edit&id[]='.$post['id'];
      $link .= '&title='.$post['title'];
      $link .= '&meno='.$post['meno'];
      $link .= '&psc='.$post['psc'];
      $link .= '&stat='.$post['stat'];
      $link .= '&created_by_alias='.$post['created_by_alias'];
      $link .= '&ico='.$post['ico'];
      $link .= '&dic='.$post['dic'];
      $link .= '&ic_dic='.$post['ic_dic'];
      $link .= '&alias='.$post['alias'];
      $link .= '&typ='.$post['typ2'];
      $link .= '&or_sud='.$post['spor_sud'];
      $link .= '&or_odd='.$post['or_odd'];
      $link .= '&or_vlozka='.$post['or_vlozka'];
      $link .= '&logo='.$post['logo'];
    }      
      
		$this->setRedirect( $link, $msg );
 	}
  
  //**** zatial nefunguje blokovanie mazania partnera pokial ma priradenu fakturu/objednavku/zmluvu
  function delete() {
    JRequest::checkToken() or die( 'Invalid Token' );
    $params	= &JComponentHelper::getParams('com_jzmluvy');
    $db 		=& JFactory::getDBO();
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    
    $where = ' WHERE catid IN('.implode($cid,',').')';
    $where_kat = ' WHERE id IN('.implode($cid,',').')';
    
    $query = 'SELECT COUNT(id)'
			. ' FROM #__jzmluvy'
			. $where
		;
		$db->setQuery( $query );
		$total = $db->loadResult();
		
    if ($total != 0) { 
      $msg 	= JText::_( 'pa_INFO_ZAZNAM' );
      $link = 'index.php?option=com_jzmluvy&view=partner';
      $this->setRedirect( $link, $msg, false );
      return false;  
    }        
    
    $query = 'DELETE FROM #__jzmluvy_partner'. $where_kat;
    
    $db->setQuery($query);
    $result = $db->query();

    $msg 	= JText::_( 'pa_INFO_ZMAZANIE' );
		$link = 'index.php?option=com_jzmluvy&view=partner';
			
		$this->setRedirect( $link, $msg );   
  }

  function publish()
  {
    global $mainframe;

    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );
	
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    $publish = 1;

    // Initialize variables
    $db		=& JFactory::getDBO();
    $user	=& JFactory::getUser();
    $uid	= $user->get('id');

    JArrayHelper::toInteger($cid);

    if (count( $cid ) < 1) {
		  $action = $publish ? 'publish' : 'unpublish';
		  JError::raiseError(500, JText::_( 'Select a category to '.$action, true ) );
    }

    $cids = implode( ',', $cid );

    $query = 'UPDATE #__jzmluvy_partner'
    . ' SET published = ' . (int) $publish
    . ' WHERE id IN ( '.$cids.' )'
    . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $uid.' ) )'
    ;
    $db->setQuery( $query );
    if (!$db->query()) {
		  JError::raiseError(500, $db->getErrorMsg() );
    }

/*	if (count( $cid ) == 1) {
		$row =& JTable::getInstance('category');
		$row->checkin( $cid[0] );
	} */

    $msg 	= JText::_( 'pa_INFO_POVOLENY' );
		$link = 'index.php?option=com_jzmluvy&view=partner';
			
		$this->setRedirect( $link, $msg );
  }
  
    function unpublish()
  {
    global $mainframe;

    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );
	
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    $publish = 0;

    // Initialize variables
    $db		=& JFactory::getDBO();
    $user	=& JFactory::getUser();
    $uid	= $user->get('id');

    JArrayHelper::toInteger($cid);

    if (count( $cid ) < 1) {
		  $action = $publish ? 'publish' : 'unpublish';
		  JError::raiseError(500, JText::_( 'Select a category to '.$action, true ) );
    }

    $cids = implode( ',', $cid );

    $query = 'UPDATE #__jzmluvy_partner'
    . ' SET published = ' . (int) $publish
    . ' WHERE id IN ( '.$cids.' )'
    . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $uid.' ) )'
    ;
    $db->setQuery( $query );
    if (!$db->query()) {
		  JError::raiseError(500, $db->getErrorMsg() );
    }

/*	if (count( $cid ) == 1) {
		$row =& JTable::getInstance('category');
		$row->checkin( $cid[0] );
	} */

    $msg 	= JText::_( 'pa_INFO_ZAKAZANY' );
		$link = 'index.php?option=com_jzmluvy&view=partner';
			
		$this->setRedirect( $link, $msg );
  }
  
  // pre triedenie riadkov
    	function saveOrder()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$this->setRedirect( 'index.php?option=com_jzmluvy&view=partner' );

		// Initialize variables
		$db			=& JFactory::getDBO();
		$cid		= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order		= JRequest::getVar( 'order', array(0), 'post', 'array' );
		$row		=& JTable::getInstance('upravapartner', '');
		$total		= count( $cid );
		$conditions	= array();
    JArrayHelper::toInteger($order, array(0));

		if (empty( $cid )) {
			return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
		}

    // update ordering values
    for( $i=0; $i < $total; $i++ )  {
		  $row->load( (int) $cid[$i] );
		  if ($row->ordering != $order[$i]) {
        $row->ordering = $order[$i];
        if (!$row->store()) {
				    JError::raiseError(500, $db->getErrorMsg() );
        }
      }
    }

    $row->reorder( );

		// Clear the component's cache
		$cache =& JFactory::getCache('com_jzmluvy');
		$cache->clean();

		$this->setMessage( JText::_('New ordering saved') );
	}
  
  function orderup() {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $db =& JFactory::getDBO();
    $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
    $row =& JTable::getInstance('upravapartner', '');
    
    $row->load($cid[0]);
    $row->move(-1);

    $this->setRedirect('index.php?option=com_jzmluvy&view=partner');
  }

  function orderdown() {
    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $db =& JFactory::getDBO();
    $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
    $row =& JTable::getInstance('upravapartner', '');
    echo $cid;
    $row->load($cid[0]);
    $row->move(1);

    $this->setRedirect('index.php?option=com_jzmluvy&view=partner');
  }

  function favorit()
  {
    global $mainframe;

    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );
	
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    $status = 1;

    // Initialize variables
    $db		=& JFactory::getDBO();
   
    JArrayHelper::toInteger($cid);

    $cid = $cid[0];

      $query = 'UPDATE #__jzmluvy_partner'
      . ' SET reserverd = ' . (int) $status
      . ' WHERE id = '.$cid
      ;
      $db->setQuery( $query );
      if (!$db->query()) {
		    JError::raiseError(500, $db->getErrorMsg() );
      }

      $msg 	= JText::_('PA_HLASKA_FAVORIT');
		  $link = 'index.php?option=com_jzmluvy&view=partner';
			
		  $this->setRedirect( $link, $msg );
  }

  function unfavorit()
  {
    global $mainframe;

    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );
	
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    $status = 0;

    // Initialize variables
    $db		=& JFactory::getDBO();

    JArrayHelper::toInteger($cid);

    $cids = implode( ',', $cid );

    $query = 'UPDATE #__jzmluvy_partner'
    . ' SET reserverd = ' . (int) $status
    . ' WHERE id IN ( '.$cids.' )'
    . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $uid.' ) )'
    ;
    $db->setQuery( $query );
    if (!$db->query()) {
		  JError::raiseError(500, $db->getErrorMsg() );
    }

    $msg 	= JText::_('PA_HLASKA_UNFAVORIT');
		$link = 'index.php?option=com_jzmluvy&view=partner';
			
		$this->setRedirect( $link, $msg );
  }
}
?>